package com.scap.authen.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

public class LoginService {

	public boolean verifyFirstLogin(HttpServletRequest request,
	HttpServletResponse response) throws Exception{
		ServletContext servletContext = request.getSession().getServletContext();
		 String relativeWebPath = "/WEB-INF/classes/login.properties";
		 String absoluteDiskPath = servletContext.getRealPath(relativeWebPath);

		 String vaUsername = "";
		 String vaPassword = "";
		 boolean fileStatus = false;
		 
		 try {
				File file = new File(absoluteDiskPath);
				FileInputStream fileInput = new FileInputStream(file);
				Properties properties = new Properties();
				properties.load(fileInput);
				fileInput.close();

				Enumeration enuKeys = properties.keys();
				while (enuKeys.hasMoreElements()) {
					String key = (String) enuKeys.nextElement();
					String value = properties.getProperty(key);
					if(key.equals("username")){
						vaUsername = value;
					}
					if(key.equals("password")){
						vaPassword = value;
					}
					
				}
				fileStatus = true;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				fileStatus = false;
			} catch (IOException e) {
				e.printStackTrace();
			}
		 
		 if(fileStatus==true){
			 if(request.getParameter("userName").equals(vaUsername) &&
					 request.getParameter("password").equals(vaPassword)){
				 fileStatus = true;
			 }else{
				 fileStatus = false;
			 }
		 }

		 return fileStatus;
	}
}
